package org.smartercontext.local.servlets;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.smartercontext.contextmger.DTO.PersonDTO;
import org.smartercontext.contextmger.contextsphere.ContextManagerController;
import org.smartercontext.contextmger.utilities.GetFriendsAndFamily;

import com.hp.hpl.jena.rdf.model.Model;

/**
 * Servlet implementation class DeleteFriendServlet
 */
@WebServlet("/DeleteFriendServlet")
public class DeleteFriendServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DeleteFriendServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
    	if(request.getQueryString()!=null){
    		HttpSession session = request.getSession();
    		String username = session.getAttribute("username").toString();
    		if(username!=null){
    			int friendsQuantity = 0;
    			String queryCopy = request.getQueryString();
    			String nameJAV = "nameJAV";
    			while(queryCopy.indexOf(nameJAV)>-1){
    				queryCopy = queryCopy.substring(queryCopy.indexOf(nameJAV)+nameJAV.length(), queryCopy.length());
    				friendsQuantity++;
    			}
    			
    			GetFriendsAndFamily gff = new GetFriendsAndFamily();
    			List<PersonDTO>pdtos = new ArrayList<>();
    			
    			for(int i=0;i<=friendsQuantity;i++){
    				String parameterName = nameJAV+i;
    				String name = request.getParameter(parameterName);
    				PersonDTO pdto = new PersonDTO();
    				pdto.setName(name);
    				pdto.setNeedDelete(true);
    				
    				pdtos.add(pdto);
    			}
    			
    			gff.setListFriends(pdtos);
    			ContextManagerController.contextManagerController();
    			
    			Model usermodel;
				try {
					
					usermodel = ContextManagerController.getUsermodel(username);
					Model usermodelM = gff.deleteFriendsToUserModel(usermodel, username);
					ContextManagerController.integrateContext(usermodelM, username);
					response.sendRedirect("manageContext_F&F.jsp");
				} catch (Exception e) {
					response.sendRedirect("manageContext_F&F.jsp?Error=3");
				}
    		}else{
    			response.sendRedirect("login.jsp?Err=3");
    		}
    	}
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

}
